
import java.io.*;
import javax.microedition.lcdui.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Bacalá
 */
class TiroBalao extends Canvas implements Runnable {

    private Balao bola, bola1, bola2;
    private Tank tank;
    private Tiro tiro;
    private Image tirim;
    private int maxX, maxY, fim, pontos = 0;

    public TiroBalao() {
        try {
            tirim = Image.createImage("/tiro1.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        maxX = getWidth();
        maxY = getHeight();
        bola = new Balao(maxX, maxY,10);
        bola1 = new Balao(maxX, maxY,20);
        bola2 = new Balao(maxX, maxY,25);
        tank = new Tank(maxX, maxY);
        tiro = new Tiro(maxX, maxY);
        tiro.invisible();
        bola.setXI(maxX / 2);
        bola.setYI(maxY / 2 - 40);
        bola1.setXI(maxX / 3);
        bola1.setYI(maxY / 3 - 30);
        bola1.setPassoX(-2);
        bola2.setXI(maxX / 4);
        bola2.setYI(maxY / 4 - 40);
        bola1.setPassoX(-1);
        tank.setX(maxX / 2 - 15);
        tank.setY(maxY - 40);
        Thread th = new Thread(this);
        th.start();

    }

    protected void paint(Graphics g) {
        if (tiro.acabouTiro()) {
            g.setColor(255, 0, 0);
            //  g.fillRect(0, 0, getWidth(), getHeight());
            if (fim < 2 * maxX | fim < 2 * maxY) {
                fim += 2;
            }
            g.fillArc((maxX - fim) / 2, (maxY - 20 - fim) / 2, fim, fim, 0, 360);
            g.setColor(255, 255, 255);
            g.drawString("GAME OVER", maxX / 2 - 30, (maxY - 55) / 2, Graphics.TOP | Graphics.LEFT);
            g.drawString("PONTOS:"+tiro.pontuacao(), maxX / 2 - 30,
                            (maxY - 25) / 2, Graphics.TOP | Graphics.LEFT);
        } else {
            g.setColor(255, 255, 255);
            g.fillRect(0, 0, getWidth(), getHeight());
            bola.paint(g);
            bola1.paint(g);
            bola2.paint(g);
            tank.paint(g);
            tiro.paint(g);
            for (int i = 0; i < tiro.getnTiro(); i++) {
                g.drawImage(tirim, 10 + (i * 5), maxY - 20, Graphics.HCENTER | Graphics.VCENTER);
            }
            g.drawString("PT:"+tiro.pontuacao(), maxX - 80, maxY - 30, Graphics.TOP | Graphics.LEFT);

        }

    }

    public void run() {

        while (true) {
            long inicio = System.currentTimeMillis();
            if (!tiro.acabouTiro()) {
                bola.mexe();
                bola1.mexe();
                bola2.mexe();
                tiro.mexe();
                bola.testaColisao(tiro);
                bola2.testaColisao(tiro);
                bola1.testaColisao(tiro);
            }
            repaint();
            long fim = System.currentTimeMillis();
            long tempo = fim - inicio;
            System.out.println(tempo);
            if (tempo < 20) {
                try {
                    Thread.sleep(20 - tempo);
                } catch (InterruptedException ie) {
                }
            }

        }

    }

    protected void keyPressed(int t) {
        if (t == KEY_NUM4) {
            tank.mexeE();
        } else if (t == KEY_NUM5) {
            tank.centraliza();
        } else if (t == KEY_NUM6) {
            tank.mexeD();
        } else if (t == KEY_NUM0) {
            if (!tiro.acabouTiro()) {
                tiro.setX(tank.getX());
                tiro.setY(tank.getY());
                tiro.visible();
                tiro.atirou();
            }
        }
        repaint();
    }

    protected void keyRepeated(int t) {
        if (t == KEY_NUM4) {
            tank.mexeE();
        } else if (t == KEY_NUM5) {
            tank.centraliza();
        } else if (t == KEY_NUM6) {
            tank.mexeD();
        } else if (t == KEY_NUM0) {
            if (!tiro.acabouTiro()) {
                tiro.setX(tank.getX());
                tiro.setY(tank.getY());
                tiro.visible();
                tiro.atirou();
            }
        }
        repaint();
    }
}
